#!C:\Python26

import sys
import os

class Plugin(object):

	capabilities =  "",							 #Indicates py file is for wscraper
	siteName = "",								  #Value for populating "Art Site" dropbox
	nameSource = []								 #namesource value passed to plugin
	artistNameEN = 0								#Determines if Artist Name input box is enabled (Boolean, 0-1)
	artistListEN = 0								#Determines if Artist List input box is enabled (Boolean, 0-1)
	overwriteModes = 0							  #Determines if overwrite mode dropbox is enabled for plugin


class PluginHandler:
	plugins = []
	pluginNames = []

	#def __init__(self, pluginpath):
	#	self.getPlugins(pluginpath)



	def get_plugins_by_capability(self, capability):
		result = []
		_instances = {}
		for plugin in Plugin.__subclasses__():
			if capability == plugin.capabilities:
				if not plugin in _instances:
					_instances[plugin] = plugin()
					result.append(_instances[plugin])
		return result

	def load_plugins(self, plugins):
		for plugin in plugins:
			#print plugin
			__import__(plugin)
		if plugins != '':
			#print plugins
			return plugins
		else:
			return 0


	def init_plugin_system(self, cfg):
		if not cfg['plugin_path'] in sys.path:
			sys.path.insert(0, cfg['plugin_path'])
		loadedplugins = self.load_plugins(cfg['plugins'])
		return loadedplugins


	def find_plugins(self):
		return Plugin.__subclasses__()

	def getPluginFilenames(self, pluginpath):
		pluginfiles = []
		for fname in os.listdir(pluginpath):
			if fname.endswith(".py"):
				pluginfiles.append(fname.rstrip(".py"))
		return pluginfiles

	def getPlugins(self, pluginpath):

		pluginfiles = self.getPluginFilenames(pluginpath)								# Get Plugin Filenames
		self.init_plugin_system({'plugin_path': pluginpath, 'plugins': pluginfiles})					# add plugin directory to #PATH variable
		instances = self.get_plugins_by_capability('wScraper')								# load the plugins

		#for pluginInstance in instances:
		#	print pluginInstance
		#for pluginInstance in Plugin.__subclasses__():
		#	print pluginInstance

		pluginInfo = {}
		for instancePtr in instances:
			#print instance
			instance = instancePtr

			if instance.capabilities >= 1.4:

				pluginInfo[instance.pluginName] =	{
										"nameSourceEN"		:	instance.nameSourceEN,
										"artistNameEN"		:	instance.artistNameEN,
										"pointer"		:	instance,
										"capabilities"		:	instance.capabilities,
										"pluginName"		:	instance.pluginName,
										"siteName"		:	instance.siteName,
										"nameSource"		:	instance.nameSource,
										"artistListEN"		:	instance.artistListEN,
										"overwriteModes"	:	instance.overwriteModes
									}
				#print pluginInfo[instance.pluginName]

			elif instance.capabilities > 1.2:
				pluginInfo[instance.pluginName] =	{
										"nameSourceEN"		:	instance.nameSourceEN,
										"artistNameEN"		:	instance.artistNameEN,
										"pointer"		:	instance,
										"capabilities"		:	instance.capabilities,
										"pluginName"		:	instance.pluginName,
										"siteName"		:	instance.siteName,
										"nameSource"		:	instance.nameSource,
										"artistListEN"		:	instance.artistListEN,
										"overwriteModes"	:	instance.overwriteModes
									}

			else :
				pluginInfo[instance.pluginName] =	{	"pointer"		:	instance,
										"capabilities"		:	instance.capabilities,
										"pluginName"		:	instance.pluginName,
										"siteName"		:	instance.siteName,
										"nameSource"		:	instance.nameSource,
										"artistListEN"		:	instance.artistListEN,
										"overwriteModes"	:	instance.overwriteModes
									}


		self.plugins = pluginInfo

		for instance in pluginInfo:
			self.pluginNames.append(pluginInfo[instance]["siteName"])			  #Generate available Plugin List for dropbox
		#print self.plugins

